<?php


function list_seragam(){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete_seragam&id=\'+id;
			}
			return false;
		}
	');
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Periode'=>array('style'=>'border-bottom:2px solid;width:15%'),  
		'Jenis Seragam'=>array('style'=>'border-bottom:2px solid;width:45%'),  
		'Nilai'=>array('style'=>'text-align:right;border-bottom:2px solid;width:25%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);

	$query = "SELECT * FROM seragam_entry a 
	INNER JOIN seragam b ON b.seragam_id=a.seragam_id 
	ORDER BY a.periode DESC";
	
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array();  
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){
		$i++;
	
		$detailproperty = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=list_seragam_karyawan&id='.$ey['seragam_entry_id'],
			'title'=>'Detail'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$editproperty = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=edit_seragam&id='.$ey['seragam_entry_id'],
			'title'=>'Edit',
			'rel'=>'facebox'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
			'href'=>'javascript:; ',
			'onclick'=>'javascript:confirmDelete('.$ey['seragam_entry_id'].');',
			'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'periode'=>  date( 'm - Y' , strtotime($ey['periode']) ) ,  
			'ns'=>  $ey['nama_seragam'] ,
			'nilai'=>  position_text_align(rp_format($ey['nilai']), 'right') ,
			'ops'=> position_text_align(  $edit_button.' '.$detail_button.' '.$delete_button , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row); 
	$menuontop = array(  
		'Tambah data'=>array('onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com=seragam_entry&task=edit_seragam\' });'),
	);
	$printmenu = additional_menu_on_list( $menuontop , '');
	$paging = $kgPagerOBJ ->showPaging();	
	return $printmenu .table_builder($header , $datas , 5, false );
} 

function form_seragam( $id ){
	$view = form_header( "seragam" , "seragam"  );
 	$fields = my_get_data_by_id('seragam_entry','seragam_entry_id',$id);

	if($fields){
		$month = date('m',strtotime($fields['periode']));
		$year = date('Y',strtotime($fields['periode']));	
	}else{
		$month = date('m');
		$year = date('Y');
	} 

	$bulans  = array(
		'01'=>'01', 
		'02'=>'02', 
		'03'=>'03', 
		'04'=>'04', 
		'05'=>'05', 
		'06'=>'06', 
		'07'=>'07', 
		'08'=>'08', 
		'09'=>'09', 
		'10'=>'10', 
		'11'=>'11', 
		'12'=>'12', 
	);
	$bulan = array(
		'name'=>'bulan',
		'value'=>isset($_POST['bulan']) ? $_POST['bulan']: $month  
	);
	$bulan_form = form_dropdown( $bulan,$bulans);	
	$tahun = array(
		'name'=>'tahun','size'=>5,
		'value'=>isset($_POST['tahun']) ? $_POST['tahun']: $year
	); 
	$tahun_form = form_dynamic($tahun );
	$view .= form_field_display( $bulan_form.' '. $tahun_form  , "Periode"    );
	
	$query = "SELECT * FROM seragam ORDER BY nama_seragam ASC";
	$result = my_query($query);
	$atemps = array();
	while( $r = my_fetch_array($result) ){
		$atemps[$r['seragam_id']] =$r['nama_seragam'];
	}	
	$temp = array(
		'name'=>'seragam_id',
		'value'=>( isset($_POST['seragam_id'] ) ? $_POST['seragam_id'] : $fields['seragam_id']  ),
		'id'=>'seragam_id' 
	);
	$form_temp = form_dropdown($temp , $atemps );
	$view .= form_field_display( $form_temp , "Jenis Seragam"    );	

	$temp = array(
		'name'=>'nilai',
		'value'=>(isset($_POST['nilai'])? $_POST['nilai'] : rp_format($fields['nilai'])),
		'id'=>'nilai',
		'type'=>'textfield',
		'size'=>'25'
	);
	$form_temp = form_money($temp); 
	$view .= form_field_display( $form_temp  , "Nilai"  );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 

	$view .= form_field_display( $form_submit , "&nbsp;" ,  "" );	
	$view .= form_footer( );
	return $view;
}

function valid_seragam($id){
	$errsubmit = false;
	$err = array();

	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function submit_seragam($id){
	$periode = $_POST['tahun'].'-'.$_POST['bulan'].'-01';
	$datas = array(
		'periode'=> my_type_data_str($periode),
		'seragam_id'=>my_type_data_int( $_POST['seragam_id']),
		'nilai'=>my_type_data_str( set_text_as_number($_POST['nilai'])),
	);
	 
	if($id > 0){	
		my_update_record('seragam_entry','seragam_entry_id',$id,$datas);
	}else{
		$id = my_insert_record('seragam_entry',$datas);
	}		
	return stor_seragam_karyawan($id);
}

function delete_seragam($id){
	$query = "DELETE FROM seragam_entry WHERE seragam_entry_id ={$id} ";
	return my_query($query);
}	

function stor_seragam_karyawan($id){
	$query = "DELETE FROM seragam_karyawan WHERE seragam_entry_id = {$id}";
	my_query($query);
	$s_entrys=my_get_data_by_id('seragam_entry','seragam_entry_id',$id) ;
	$seragam_id=isset( $s_entrys['seragam_id'] ) ? $s_entrys['seragam_id']:  0;

	$query = "SELECT karyawan_id FROM kekaryawanan 
		WHERE jabatan_id IN (select jabatan_id FROM jabatan WHERE seragam_id={$seragam_id})";
	$result = my_query($query);
	while( $ey = my_fetch_array($result) ){
		$datas = array(
			'seragam_entry_id'=>my_type_data_int($id),
			'karyawan_id'=>my_type_data_int($ey['karyawan_id']),
		);	
		my_insert_record('seragam_karyawan' , $datas);	
	}
	return true;
}

/*
	==================================
		SERAGAM KARYAWAN
	==================================
*/
function list_seragam_karyawan($id){
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:15%'),  
		'Nama Karyawan'=>array('style'=>'border-bottom:2px solid;width:40%'),  
		'Jabatan'=>array('style'=>'border-bottom:2px solid;width:40%'),  
	);
	
	$query = "SELECT * FROM seragam_karyawan a
		INNER JOIN karyawan b ON b.karyawan_id=a.karyawan_id 
		WHERE a.seragam_entry_id=".$id. 
		" ORDER BY b.nik ASC ";
	$result = my_query($query);
	$row = array();
	$i = 0;
	while( $ey = my_fetch_array($result) ){
		$i++;
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		$jabatan = loaddata_jabatan($karyawan['jabatan_id']);
		$row[] = array( 
			'#'=>position_text_align ($i, 'center'), 
			'nik'=>  $karyawan['nik'] ,
			'nama'=>  $karyawan['nama_gelar'] ,   
			'jabatan'=>  $jabatan['nama_jabatan'] ,   
		); 
	}
	
	$datas = table_rows($row); 
	$view = form_header( "..." , "..."  );
	$sks=my_get_data_by_id('seragam_entry','seragam_entry_id',$id);
	$view .= form_field_display( date("m-Y", strtotime($sks['periode']))   , "Periode"   );
	$seragam_id=isset( $sks['seragam_id'] ) ? $sks['seragam_id']:  0;
	$seragams=my_get_data_by_id('seragam','seragam_id',$seragam_id);
	$view .= form_field_display( $seragams['nama_seragam']   , "Jenis Seragam"   );
	$view .= form_footer( );
	$menuontop = array(  
		'Kembali'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'\';'),
		'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com='.$_GET['com'].'&task=cetak_seragam_karyawan&id='.$id.'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');'),
		'Excel'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=excel_seragam_karyawan&id='.$id.'\';'),
	);
	$printmenu = additional_menu_on_list( $menuontop , array()  );
	return  $view.$printmenu .table_builder( 	$header , 	$datas , 	5 , 	false );
}


function excel_seragam_karyawan($id){
	my_component_load('xl_builder' , false);
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:15%'),  
		'Nama Karyawan'=>array('style'=>'border-bottom:2px solid;width:40%'),  
		'Jabatan'=>array('style'=>'border-bottom:2px solid;width:40%'),  
	);
	$query = "SELECT * FROM seragam_karyawan a
		INNER JOIN karyawan b ON b.karyawan_id=a.karyawan_id 
		WHERE a.seragam_entry_id=".$id. 
		" ORDER BY b.nik ASC ";

	$result = my_query($query ); 
	$row = array();  
	$i=0;
	while($ey = my_fetch_array($result)){
		$i++;
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		$jabatan = loaddata_jabatan($karyawan['jabatan_id']);
		$row[] = array( 
			'#'=>position_text_align ($i, 'center'), 
			'nik'=>  $karyawan['nik'] ,
			'nama'=>  $karyawan['nama_gelar'] ,   
			'jabatan'=>  $jabatan['nama_jabatan'] ,   
		); 
		
	}	
	$view = form_header( "..." , "..."  );
	$sks=my_get_data_by_id('seragam_entry','seragam_entry_id',$id);
	$view .= form_field_display( date("m-Y", strtotime($sks['periode']))   , "Periode"   );
	$seragam_id=isset( $sks['seragam_id'] ) ? $sks['seragam_id']:  0;
	$seragams=my_get_data_by_id('seragam','seragam_id',$seragam_id);
	$view .= form_field_display( $seragams['nama_seragam']   , "Jenis Seragam"   );	
	$datas = table_rows_excel($row); 
	return $view.table_builder_excel($header , $datas , 15 ,false ); 
}

function cetak_seragam_karyawan($id){
	$sks=my_get_data_by_id('seragam_entry','seragam_entry_id',$id);
	$seragam_id=isset( $sks['seragam_id'] ) ? $sks['seragam_id']:  0;
	$seragams=my_get_data_by_id('seragam','seragam_id',$seragam_id);
	
	$query = "SELECT * FROM seragam_karyawan a
		INNER JOIN karyawan b ON b.karyawan_id=a.karyawan_id 
		WHERE a.seragam_entry_id=".$id. 
		" ORDER BY b.nik ASC ";	
	$result = my_query($query ); 
	$datas = array();  

	$datas[] = array(
		 'k1'=>array("Periode " ,10 ,STR_PAD_RIGHT),
		 'k2'=>array(": ".date("m-Y", strtotime($sks['periode'])) ,10 ,STR_PAD_RIGHT),
		 'k3'=>array("Jenis Seragam " ,15 ,STR_PAD_RIGHT),
		 'k4'=>array(": ".$seragams['nama_seragam'] ,65 ,STR_PAD_RIGHT),
	); 
	$datas[] = array(
		 'k1'=>array(str_repeat("=", 100) ,100 ,STR_PAD_BOTH),
	); 

	$datas[] = array(
		 'k1'=>array("No." ,5 ,STR_PAD_BOTH),
		 'k2'=>array("NIK" ,15,STR_PAD_RIGHT),
		 'k3'=>array("Nama Karyawan" ,40,STR_PAD_RIGHT),  
		 'k4'=>array("Jabatan" ,40,STR_PAD_RIGHT),  
	); 
	$datas[] = array(
		 'k1'=>array(str_repeat("=", 100) ,100 ,STR_PAD_BOTH),
	); 
	$i=0;
	while($ey = my_fetch_array($result)){
		$i++; 
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		$jabatan = loaddata_jabatan($karyawan['jabatan_id']);
		$datas[] = array(		 
			'k1'=>array($i ,5 ,STR_PAD_BOTH),
			'k2'=>array($karyawan['nik'] ,15,STR_PAD_RIGHT),
			'k3'=>array($karyawan['nama_gelar'],40,STR_PAD_RIGHT),  
			'k4'=>array($jabatan['nama_jabatan'] ,40,STR_PAD_RIGHT),  
		);
	}
	$view = 	str_pad("CETAK SERAGAM KARYAWAN",100," ",STR_PAD_BOTH);
	return $view ."\n"."\n".set_txt_report($datas);
}

	